package com.uenpay.oem.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.android.arouter.launcher.ARouter;
import com.qyx.android.message.type.MsgContentType;
import com.uenpay.im.IMApplication;
import com.uenpay.im.ui.old.activity.PushServiceConn;
import com.uenpay.oem.ui.user.welcome.WelcomeActivity;
import com.uenpay.oem.R;
import com.uenpay.oem.ui.main.MainActivity;
import com.uenpay.oem.ui.user.login.LoginActivity;
import com.uenpay.util.app.base.BaseActivity;

/**
 * 程序首页
 */
public class FirstActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        isHideStatuBar= true;
        super.onCreate(savedInstanceState);
        boolean isFromNotification = getIntent().getBooleanExtra("notification", false);
        if (isFromNotification) {
            if (!IMApplication.getIsLogoutConfig()) {
                MainActivity.launch(this);
                int category = getIntent().getIntExtra("category", 0);
                if (category == MsgContentType.FRIEND_INVITE) {
                    ARouter.getInstance().build("/im/new_friend").navigation(this);
                }
                finish();
                return;
            } else {
                LoginActivity.launch(this);
                this.finish();
                return;
            }
        }

        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
            finish();
            return;
        }

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (IMApplication.getFirstTime()) {
                    //WelcomeActivity.launch(FirstActivity.this);
                    LoginActivity.launch(FirstActivity.this);
                } else if (IMApplication.getIsLogoutConfig() || TextUtils.isEmpty(IMApplication.getUserMobilePhone())
                        || TextUtils.isEmpty(IMApplication.getAccessToken())) {
                    LoginActivity.launch(FirstActivity.this);
                } else {
                    signIn();
                }
                finish();
            }
        }, 300);
        setSwipeBackEnable(false);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_first;
    }

    @Override
    public void initView(View view) {

    }

    private void signIn() {
        /** 连接socket **/
        PushServiceConn.getInstance(FirstActivity.this).startConn();
        MainActivity.launch(this);
        finish();
    }
}
